<template>
	<view>
		<view class="logo">
			<image :src="baseUrl + 'images/static/login/loginBK.png'" mode="" class="img"></image>
			<!-- <view class="login">登录</view> -->
		</view>
			<!-- 	<view class="wxlogin"><u-button type="success" @click="wxLogin" shape="circle" open-type="getPhoneNumber">微信一键登录</u-button></view> -->
		<!-- <view class="phonelogin"><u-button 	 @click="phoneLogin" shape="circle">手机号登录</u-button></view> -->
		<button type="warn" class="phone" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">授权登录</button>

	</view>

</template>

<script>
	import {
		request,
		baseUrl
	} from '../../http/request.js';
	export default {
		data() {
			return {
			baseUrl:baseUrl,
			}
		},
		onShow() {
		
		},
		methods: {
			
			 getPhoneNumber(e) {
				uni.login({
					"provider": "weixin",
					"onlyAuthorize": true,
					success: async (loginRes) => {
						console.log('微信一键登录返回的信息', loginRes.code)
						if (e.detail.errMsg === 'getPhoneNumber:ok') {
						    // 授权成功，处理逻辑
						    console.log("获取手机号成功", e.detail)
							let data = await this.$http.LoginHttp.phoneLogin({
								code:loginRes.code,
								encryptedData: e.detail.encryptedData,
								iv: e.detail.iv
							})
							console.log("返回的数据",data)
							if (data.code == 200) {
								uni.$u.toast("登录成功")
								uni.setStorageSync("_id", data.userInfo._id)
								uni.setStorageSync("token", data.token)
								uni.setStorageSync("openid", data.openid)
								uni.setStorageSync("phoneNumber", data.userInfo.phoneNumber)
								setTimeout(function() {
								    uni.switchTab({
								        url: '/pages/Home/Home'
								    });
								}, 1000)
							}
						} else {
						    // 授权失败，处理逻辑
						    console.log("获取手机号失败", e.detail)
						    if (e.detail.errMsg === 'getPhoneNumber:fail no permission') {
						      
						        uni.showToast({
						            title: '获取手机号授权失败，请重新授权',
						            icon: 'none'
						        });
						    }
						}
						
						
						},
						
						
						})
		
			
			},

			wxLogin() {
				uni.getUserInfo({
					desc: '登录数据',
					success: (res) => {
						console.log('用户信息', res)
						// uni.setStorageSync("avatarUrl", res.userInfo.avatarUrl)
						// uni.setStorageSync("nickName", res.userInfo.nickName)
						uni.login({
							"provider": "weixin",
							"onlyAuthorize": true,
							success: async (loginRes) => {
								console.log('微信一键登录返回的信息', loginRes.code)
								// 将 code 发送到后端
								let data = await this.$http.LoginHttp.wxLogin({
									code: loginRes.code,
									encryptedData: res.encryptedData,
									iv: res.iv
								})
								console.log('后端返回的数据', data)
								if (data.code == 200) {
									uni.$u.toast("登录成功")
									uni.setStorageSync("_id", data.userInfo._id)
									uni.setStorageSync("token", data.token)
									uni.setStorageSync("openid", data.openid)
									uni.setStorageSync("avatarUrl", data.userInfo.avatarUrl)
									uni.setStorageSync("nickName", data.userInfo.nickname)
									uni.switchTab({
										url: '/pages/mine/mine'
									})
								}

							}
						})
					}
				})
			},
			//手机号登录
			phoneLogin() {
				uni.navigateTo({
					url: '/pagesLogin/register/phoneLogin',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.logo {
		width: 100%;
		text-align: center;
		margin: 100rpx 0rpx;
	}

	.img {
		width: 606rpx;
		height: 520rpx;


	}

	.login {
		font-size: 40rpx;
		font-weight: bold;
	}

	.wxlogin {
		width: 600rpx;
		margin: 0 auto;
	}

	.phonelogin {
		width: 600rpx;
		margin: 0 auto;
		margin-top: 30rpx;
	}

	.phone {
		width: 560rpx;
		height: 96rpx;
		background: #31AC38;
		margin: 0 auto;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		font-weight: 500;
		font-size: 30rpx;
		color: #FFFFFF;
		line-height: 96rpx;
		text-align: center;
	}
</style>